package com.xn.studydemo.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author xn
 * @date 2022/11/21 19:20
 */
@RestController
public class MessageAckController {

    @Resource
    RabbitTemplate rabbitTemplate;

    /**
     * 这个一个错误的示例，它会报错 没有找到交换机'non-existent-exchange'
     */
    @PostMapping("/ErrorTestMessageAck")
    public String ErrorTestMessageAck() {
        //构建消息
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);

        //发送消息
        rabbitTemplate.convertAndSend("non-existent-exchange", "lonelyDirectRouting", map);
        return "发送成功";
    }

    /**
     * 测试lonelyDirect的消息ack
     *
     * 如果在消费者还没有启动或没有配置队列的时候，在RetrunCallback回调函数的打印参数里面可以看到，消息是推送到了交换机成功了，但是在路由分发给队列的时候，找不到队列，所以报了错误 NO_ROUTE 。
     */
    @PostMapping("/TestMessageAck")
    public String TestMessageAck1() {
        //构建消息
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);

        //发送消息
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "lonelyDirectRouting", map);
        return "发送成功";
    }

    /**
     *  测试TestDirect的消息ack
     */
    @PostMapping("/TestMessageAck2")
    public String TestMessageAck2() {
        System.out.println("我开始了");
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);

        System.out.println(map);

        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "发送成功";
    }

    /**
     *  测试fanout的消息ack
     */
    @PostMapping("/TestMessageAck3")
    public String TestMessageAck3() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);

        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return "发送成功";
    }
}
